﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Commands &amp; events</title>
  <keywords>
    <keyword translate="true">Commands</keyword>
    <keyword translate="true">Events</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Commands &amp; events</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Commanding is perfectly supported by Catel. Catel supports Command classes, which is also known as </text><text styleclass="Normal" style="font-style:italic;" translate="true">RelayCommand</text><text styleclass="Normal" translate="true"> or </text><text styleclass="Normal" style="font-style:italic;" translate="true">DelegateCommand</text><text styleclass="Normal" translate="true"> in other frameworks. Defining a command on a view model is very easy, as you can see in the code below:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">// TODO: Move code below to constructor</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">Edit = </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Command(OnEditExecute, OnEditCanExecute);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">// TODO: Move code above to constructor</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Gets the Edit command.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Command Edit { get; </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> set; }</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Method to check whether the Edit command can be executed.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">bool</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> OnEditCanExecute()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">return</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">true</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Method to invoke when the Edit command is executed.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> OnEditExecute()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">// TODO: Handle command logic here</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are some people who don’t like the </text><text styleclass="Normal" style="font-style:italic;" translate="true">ICommand</text><text styleclass="Normal" translate="true"> implementations. For example, Caliburn (Micro) uses convention and does not require the creation of a command. There are a few downsides for that:</text></para>
    <list id="1" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">It requires you to make sure the name of the control is the same as the method;</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">It is not clear that it is actually a command if you are not fully familiar with the conventions;</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The methods need to be public (otherwise, how are you going to invoke the commands during unit tests?), which make them freely available (and that’s not something we like);</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">You will always have to invoke CanExecute yourself again in Execute, because you have no guarantee that the source of Execute is actually the convention mapping;</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">There is no way to manually refresh the CanExecute state on the bound controls.</text></li>
    </list>
  </body>
</topic>
